package interview.sync.lock;
/*

 */

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SaleTicketkt
 * @Author Final
 * @Project DataStructures
 */

/*
    第一步，创建资源类，定义属性和操作方法
*/
class Ticket{
    //票数
    private Integer number = 900;

    private final ReentrantLock lock=new ReentrantLock();

    //操作方法
    public void sale(){

        lock.lock();
        //判断是否有票
        try {

            if (number>0){
                System.out.println(Thread.currentThread().getName()+"卖出"+(number--)+"剩下"+number);
            }
        }finally {

            lock.unlock();
        }


    }

}

public class SaleTicketkt {
    Ticket ticket=new Ticket();

    //创建多个线程，调用资源类操作方法
    public static void main(String[] args) {
        Ticket ticket=new Ticket();


        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<=90;i++){
                    ticket.sale();
                }
            }
        },"xzl").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<30;i++){
                    ticket.sale();
                }
            }
        },"cxm").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<30;i++){
                    ticket.sale();
                }
            }
        },"cyl").start();
    }
}
